package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"strings"

	"github.com/PuerkitoBio/goquery"
)

var (
	filename string
	outfile  string
)

func init() {
	flag.StringVar(&filename, "i", "urls.txt", "url input file")
	flag.StringVar(&outfile, "o", "hosts.txt", "hosts out file")

}

//文件解析
func readHosts(filename string) ([]string, error) {
	// 判断文件是否存在
	if _, err := os.Stat(filename); os.IsNotExist(err) {
		return nil, err
	}
	hosts := []string{}                     //定义未知长度切片
	lines, err := ioutil.ReadFile(filename) //读取文件
	if err != nil {
		return nil, err
	} else {
		contents := string(lines)              //讲数据转换为字符串
		lines := strings.Split(contents, "\n") //根据换号切片
		for _, line := range lines {
			line = strings.TrimSpace(line)
			if len(line) > 3 {
				hosts = append(hosts, line)
			}
		}
	}
	return hosts, err
}

//
func hostParser(host string) (string, error) {
	url := "http://" + host + ".ipaddress.com"
	client := &http.Client{}
	req, _ := http.NewRequest("GET", url, nil)
	// 自定义Header
	req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36")
	resp, err := client.Do(req)
	if err != nil {
		return "", err
	}
	//函数结束后关闭相关链接
	defer resp.Body.Close()
	dom, err := goquery.NewDocumentFromReader(resp.Body)
	if err != nil {
		return "", err
	}
	var (
		ip  = ""
		txt = ""
	)
	dom.Find(".comma-separated").Eq(0).Children().Each(func(i int, selection *goquery.Selection) {
		ip = selection.Text()
		txt = txt + ip + "\t" + host + "\r\n"
	})
	return txt, nil
}

func main() {
	flag.Parse()
	txt := ""

	hosts, err := readHosts(filename)
	if err != nil {
		log.Fatalln(err)
		return
	}
	for _, hostname := range hosts {
		t, err := hostParser(hostname)
		if err != nil {
			fmt.Println("host", hostname)
			log.Fatalln(err)
		} else {
			fmt.Println(t)
			txt = txt + t
		}
	}
	str := []byte(txt)
	err = ioutil.WriteFile(outfile, str, 0644)
	if err != nil {
		panic(err)
	}
}
